[#include "../../activiti/common/html.ftl" /]
[@html]
    [@head_activiti title="流程配置" css=["fw", "bootstrap-table"] /]
    [@body]
        [#--主体内容--]
        [@main ]
            <div class="am-g am-radius fw-tables">
                [#--表格主体内容--操作栏--]
                [@mainHeader]
                    [#--[@shiro.hasPermission name="activiti:procdef:add"]--]
                    <button class="am-btn am-btn-success am-radius" onclick="$.operate.addFull()">添加</button>
                    [#--[/@shiro.hasPermission]--]
                    [#--[@shiro.hasPermission name="activiti:procdef:remove"]--]
                    <button class="am-btn am-btn-danger am-radius multiple am-disabled" onclick="$.operate.removeAll('prompt')">删除</button>
                    [#--[/@shiro.hasPermission]--]
                    [#--[@shiro.hasPermission name="activiti:procdef:remove"]--]
                    <button class="am-btn am-btn-danger am-radius multiple am-disabled" onclick="suspendAll()">停用</button>
                    [#--[/@shiro.hasPermission]--]
                    [#--[@shiro.hasPermission name="activiti:procdef:remove"]--]
                    <button class="am-btn am-btn-warning am-radius multiple am-disabled" onclick="shareAll()">分享</button>
                    [#--[/@shiro.hasPermission]--]
                    [#--[@shiro.hasPermission name="activiti:procdef:remove"]--]
                    <button class="am-btn am-btn-warning am-radius multiple am-disabled" onclick="cancelShareAll()">取消分享</button>
                    [#--[/@shiro.hasPermission]--]
                [/@mainHeader]

                [#--表格主体内容--搜索栏--]
                [@searchForm searchId="form-prodef-search" ]
                    <div class="am-form-group">
                        <input type="text" name="procKey" class="am-form-field am-input-sm am-radius" placeholder="流程KEY">
                    </div>
                    <div class="am-form-group">
                        <input type="text" name="procName" class="am-form-field am-input-sm am-radius" placeholder="流程名称">
                    </div>
                    <div class="am-form-group">
                        <select name="category" class="am-radius" data-am-selected placeholder="流程分类">
                            <option value=""></option>
                            [#list dict.getType("act_re_procdef_category") as dict]
                                <option value="${dict.dictValue}">${dict.dictLabel}</option>
                            [/#list]
                        </select>
                    </div>
                [/@searchForm]

                <div class="fw-table">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        [/@main]

        [#--PC/APP 右侧悬浮按钮--]
        [@rightToolbar /]

        [@script_activiti js=["fw", "bootstrap-table"]]
        [/@script_activiti]
        <script type="text/javascript">
            [#--var editFlag = "${hasPermission('activiti:procdef:view')}";--]
            [#--var removeFlag = "${hasPermission('activiti:procdef:remove')}";--]
            var editFlag = "";
            var removeFlag ="";
            var stateDatas = ${convert.toJson(dict.getType("act_suspension_state"))};
            var categoryDatas = ${convert.toJson(dict.getType("act_re_procdef_category"))};
            var isShareDatas = ${convert.toJson(dict.getType("act_re_procdef_is_share"))};
            var shareStateDatas = ${convert.toJson(dict.getType("act_re_procdef_share_state"))};
            var isAdministrator = ${convert.toJson(common.isAdministrator())};

            var prefix = ctx + "osfw/activiti/procdef";

            $(function () {
                var options = {
                    url: prefix + "/list",
                    createUrl: prefix + "/addSimple",
                    updateUrl: prefix + "/edit/{id}",
                    removeUrl: prefix + "/remove",
                    shareUrl: prefix + "/share",
                    cancelShareUrl: prefix + "/cancelShare",
                    suspendUrl: prefix + "/suspend",
                    activateUrl: prefix + "/activate",
                    releaseUrl: prefix + "/release",
                    fallbackUrl: prefix + "/fallback",
                    getNextNodeInfoUrl: prefix + "/getNextNodeInfo",
                    sortName: "createTime",
                    sortOrder: "desc",
                    modalName: "流程",
                    uniqueId: "procDefId",
                    columns: [
                        {
                            checkbox: true
                        },
                        {
                            title: "序号",
                            formatter: function (value, row, index) {
                                return $.table.serialNumber(index);
                            }
                        },
                        {
                            field: 'procName',
                            title: '流程名称',
                            sortable: true,
                            formatter: function (value, row, index) {
                                return $.table.tooltip(value, 20, "copy");
                            }
                        },
                        {
                            field: 'procKey',
                            title: '流程KEY',
                            sortable: true,
                            formatter: function (value, row, index) {
                                return $.table.tooltip(value, 10, "copy");
                            }
                        },
                        {
                            field: 'version',
                            title: '版本号',
                            sortable: true,
                            formatter: function (value, row, index) {
                                return $.common.isEmpty(value) ? value : 'V'+value;
                            }
                        },
                        {
                            field: 'deployTime',
                            title: '部署时间',
                            sortable: true,
                        },
                        {
                            field: 'category',
                            title: '流程类别',
                            visible: false,
                            formatter: function (value, row, index) {
                                return $.table.selectDictLabel(categoryDatas, value);
                            }
                        },
                        {
                            field: 'deptName',
                            title: '所属部门',
                            visible: false
                        },
                        {
                            field: 'shareState',
                            title: '是否共享',
                            formatter: function (value, row, index) {
                                if(value == 0) {
                                    return $.table.selectDictLabel(isShareDatas, 0);
                                }
                                return $.table.selectDictLabel(isShareDatas, 1);
                            }
                        },
                        {
                            field: 'shareState',
                            title: '共享详情',
                            visible: false,
                            formatter: function (value, row, index) {
                                return $.table.selectDictLabel(shareStateDatas, value);
                            }
                        },
                        {
                            field: 'suspensionState',
                            title: '状态',
                            formatter: function (value, row, index) {
                                return $.table.selectDictLabel(stateDatas, value);
                            }
                        },
                        {
                            field: 'deleteReason',
                            title: '停用原因',
                            visible: false,
                            formatter: function (value, row, index) {
                                return $.table.tooltip(value, 10);
                            }
                        },
                        {
                            title: '操作',
                            cellStyle: operCellStyle,
                            formatter: function (value, row, index) {
                                var actions = [];
                                actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editFull(\'' + row.procDefId + '\')">编辑</a>');
                                if(row.suspensionState == 0) {
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="release(\'' + row.procDefId + '\')">发布</a>');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.procDefId + '\', \'prompt\')">删除</a>');
                                }else if(row.suspensionState == 1) {
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="suspend(\'' + row.procDefId + '\')">停用</a>');
                                    if(row.shareState == 0) {
                                        actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="share(\'' + row.procDefId + '\')">分享</a>');
                                    }else if(row.shareState == 1 || row.shareState == 2) {
                                        actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="cancelShare(\'' + row.procDefId + '\')">取消分享</a>');
                                    }
                                    if(row.version > 1) {
                                        actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="fallback(\'' + row.procDefId + '\',\'' + row.procKey + '\',' + row.version + ')">回退</a>');
                                    }
                                }else if(row.suspensionState == 2) {
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="activate(\'' + row.procDefId + '\')">启用</a>');
                                    if(row.shareState == 1 || row.shareState == 2) {
                                        actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="cancelShare(\'' + row.procDefId + '\')">取消分享</a>');
                                    }
                                }
                                return actions.join('');
                            }
                        }]
                };
                $.table.init(options);
            });

            // 挂起
            var suspend= function(id) {
                table.set();
                $.modal.prompt("确定停用该条" + table.options.modalName + "配置吗？", "请输入您要停用的理由：", "", "textarea", function (data) {
                    var url = table.options.suspendUrl;
                    var data = { "ids": id, "deleteReason": data || '' };
                    $.operate.submit(url, "post", "json", data);
                }, "cancel");
            };
            // 批量挂起
            var suspendAll = function() {
                table.set();
                var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
                if (rows.length == 0) {
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }
                $.modal.prompt("确认要停用选中的" + rows.length + "条数据吗?", "请输入您要停用的理由：", "", "textarea", function (data) {
                    var url = table.options.suspendUrl;
                    var data = { "ids": rows.join(), "deleteReason": data || '' };
                    $.operate.submit(url, "post", "json", data);
                }, "cancel");
            };

            // 分享
            var share= function(id) {
                table.set();
                $.modal.prompt("确定分享该条" + table.options.modalName + "配置吗？", "请选择您要分享的级别：", "", "select", function (data) {
                    var url = table.options.shareUrl;
                    var data = { "ids": id, "shareState": data.val() || '1' };
                    $.operate.submit(url, "post", "json", data);
                }, "cancel", shareStateDatas, "1");
            };
            // 取消分享
            var cancelShare= function(id) {
                table.set();
                $.modal.confirm("确定取消分享该条" + table.options.modalName + "配置吗？", function () {
                    var url = table.options.cancelShareUrl;
                    var data = { "ids": id };
                    $.operate.submit(url, "post", "json", data);
                });
            };
            // 批量分享
            var shareAll = function() {
                table.set();
                var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
                if (rows.length == 0) {
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }
                $.modal.prompt("确认要分享选中的" + rows.length + "条数据吗?", "请选择您要分享的级别：", "", "select", function (data) {
                    var url = table.options.shareUrl;
                    var data = { "ids": rows.join(), "shareState": data.val() || '1' };
                    $.operate.submit(url, "post", "json", data);
                }, "cancel", shareStateDatas, "1");
            };
            // 批量取消分享
            var cancelShareAll = function() {
                table.set();
                var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
                if (rows.length == 0) {
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }
                $.modal.confirm("确认要取消分享选中的" + rows.length + "条数据吗?", function () {
                    var url = table.options.cancelShareUrl;
                    var data = { "ids": rows.join() };
                    $.operate.submit(url, "post", "json", data);
                });
            };

            // 激活
            var activate= function(id) {
                table.set();
                $.modal.confirm("确定激活该条" + table.options.modalName + "配置吗？", function () {
                    var url = table.options.activateUrl;
                    var data = { "id": id };
                    $.operate.submit(url, "post", "json", data);
                });
            };

            // 激活
            var release= function(id) {
                table.set();
                $.modal.confirm("确定发布该条" + table.options.modalName + "配置吗？", function () {
                    var url = table.options.releaseUrl;
                    var data = { "id": id };
                    $.operate.submit(url, "post", "json", data);
                });
            };

            // 回退
            var fallback= function(id, procKey, version) {
                table.set();
                // 查询版本列表 todo
                var option = '<option value="" selected>-- 请选择 --</option>\n';
                $.operate.get(prefix + "/selectProcdefByProcKey?prockey="+procKey, function (res) {
                    $.each(res.data, function (index, item) {
                        console.log(item.version, procKey, version);
                        if(item.version == version) {
                            option += '<option value="'+item.version+'" disabled readonly style="background-color: #eee;color: #548DFF;"> 版本号 [ V'+item.version+' ] --- 流程名称 [ '+item.procName+' ]  （当前版本）</option>\n';
                        }  else {
                            option += '<option value="'+item.version+'"> 版本号 [ V'+item.version+' ] --- 流程名称 [ '+item.procName+' ]</option>\n';
                        }
                    });
                    $.modal.prompt("确定回退该条" + table.options.modalName + "版本吗？", "请选择您要回退的版本号及回退原因：", "", "select,textarea", function (data) {
                        var url = table.options.fallbackUrl;
                        var data = { "id": id, "fallbackVersion": data[0].val(), "deleteReason": data[1]};
                        $.operate.submit(url, "post", "json", data);
                    }, "cancel", option, "", "请在此处填写回退原因");
                });
            };

        </script>
    [/@body]
[/@html]
